package org.datastealer.util;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.text.SimpleDateFormat;
import java.util.TimeZone;

/**
 * Note：
 *
 * @author ：mengjw
 * @description：Json工具类
 * @date ：Created in 2020/9/13
 */
public class JsonUtils {

    private static final Object LOCK = new Object();
    private static ObjectMapper mapper = null;

    /**
     * 提供统一的ObjectMapper，使用双重检查锁定
     *
     * @return
     */
    public static ObjectMapper getObjectMapper() {
        if (mapper == null) {
            synchronized (LOCK) {
                if (mapper == null) {
                    mapper = new ObjectMapper();
                    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                    mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
                    mapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
                    // 可以考虑全部转下划线展示
                    /*mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);*/
                    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
                }
            }
        }
        return mapper;
    }


}